package naga.x.platform2.channel.egret;

import java.util.Map;
import java.util.TreeMap;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;

import naga.x.platform2.app.Channel;
import naga.x.platform2.app.model.Recharge;
import naga.x.platform2.app.support.RechargeSupport;
import naga.x.platform2.channel.egret.support.AccountResponse;
import naga.x.platform2.channel.egret.support.EgretApi;
import naga.x.platform2.channel.egret.support.EgretConfig;
import naga.x.platform2.channel.egret.support.PayResult;
import naga.x.platform2.channel.egret.support.Response;
import naga.x.platform2.channel.egret.support.ResponseCode;
import naga.x.platform2.support.RequestPath;
import naga.x.platform2.utils.HttpUtils;
import naga.x.platform2.utils.NumberUtils;
import naga.x.platform2.utils.StringUtils;

/**
 * 白鹭
 * @see <a href="http://open.egret.com/Wiki">白鹭wiki</a>
 * @author yang.li
 *
 */
@Controller
public class EgretController {

	private static final Logger LOG = LoggerFactory.getLogger(EgretController.class);
	
	@Autowired
	private EgretConfig config;
	@Autowired
	private RechargeSupport support;
	
	private static Channel channel = Channel.EGRET;
	
	@RequestMapping(path = { RequestPath.EGRET_PAY_CALLBACK, RequestPath.EGRET_PAY_CALLBACK1 }, method = RequestMethod.POST, produces = "application/json; charset=UTF-8")
	@ResponseBody
	public Response pay(PayResult req) {
		if (req == null) {
			LOG.error("[EGRET RECHARGE ERROR]CAUSE[NULL]");
			return new Response(ResponseCode.PARAMETER_ERROR);
		}
		if (!req.validate()) {
			LOG.error("[EGRET RECHARGE ERROR]{}CAUSE[PARAM ERROR]", req.toString());
			return new Response(ResponseCode.PARAMETER_ERROR);
		}
		String sign = EgretApi.makeSign(req.toMap(), config.getAppKey());
		if (StringUtils.isEmpty(sign) || !sign.equals(req.getSign())) {
			LOG.error("[EGRET RECHARGE ERROR]{}CAUSE[SIGN ERROR]", req.toString());
			return new Response(ResponseCode.SIGN_ERROR);
		}
		long uid = NumberUtils.parseLong(req.getExt());
		Recharge recharge = new Recharge(req.getOrderId(), uid/*ext为游戏服角色id*/, req.getServerId(), req.getMoney(), -1, req.getGoodsId(), channel.name);
		// 不是正确的uid，则利用渠道用户id
		recharge.setChannelUid(req.getId());
		if (support.offer(recharge)) {
			LOG.info("[EGRET RECHARGE SUCC]{}", req.toString());
			return new Response(ResponseCode.SUCC);
		}
		LOG.info("[EGRET RECHARGE FAIL]{}CAUSE[DUPLICATE]", req.toString());
		return new Response(ResponseCode.ORDER_DUPLICATE);
	}
	
	@RequestMapping(path = { RequestPath.EGRET_GET_ACCOUNT, RequestPath.EGRET_GET_ACCOUNT1 }, method = RequestMethod.POST, produces = "application/json; charset=UTF-8")
	@ResponseBody
	public AccountResponse getAccount(String token) {
		if (StringUtils.isEmpty(token)) {
			return new AccountResponse(EgretApi.CODE_ERROR, "token错误");
		}
		Map<String, String> params = new TreeMap<String, String>();
		params.put("appId", config.getAppId());
		params.put("time", String.valueOf(System.currentTimeMillis() / 1000));
		params.put("token", token);
		params.put("sign", EgretApi.makeSign(params, config.getAppKey()));
		String url = config.getReqUserUrl();
		String json = HttpUtils.post(url, params);
		if (StringUtils.isEmpty(json)) {
			return new AccountResponse(EgretApi.CODE_ERROR, "获取用户信息失败");
		}
		try {
			AccountResponse res = JSON.parseObject(json, AccountResponse.class);
			return res.compatible();
		} catch (Exception e) {
			LOG.error("[EGRET GET ACC ERROR]" + token, e);
		}
		return new AccountResponse(EgretApi.CODE_ERROR, "获取用户信息失败");
	}
	
}
